package GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class An1 extends JFrame implements ItemListener {
    //声明并创建RadioButton对象
    private JRadioButton radioButton1 = new JRadioButton("男");
    private JRadioButton radioButton2 = new JRadioButton("女");
    public An1(String title) {
        super(title);
// 设置布局管理BorderLayout
        getContentPane().setLayout(new BorderLayout());
// 创建一个面板panel1放置TextField和Password
        JPanel panel1 = new JPanel();
        FlowLayout flowLayout_1 = (FlowLayout) panel1.getLayout();
        flowLayout_1.setAlignment(FlowLayout.LEFT);
// 将面板panel1添加到内容视图
        getContentPane().add(panel1, BorderLayout.NORTH);
// 创建标签
        JLabel lblTextFieldLabel = new JLabel("选择你喜欢的编程语言：");
// 添加标签到面板panel1
        panel1.add(lblTextFieldLabel);
        JCheckBox checkBox1 = new JCheckBox("Java");
        panel1.add(checkBox1);
        JCheckBox checkBox2 = new JCheckBox("C++");
        panel1.add(checkBox2);
        JCheckBox checkBox3 = new JCheckBox("Objective-C");
//注册checkBox3对ActionLEvent事件监听
        checkBox3.addActionListener(event -> {
// 打印checkBox3状态
            System.out.println(checkBox3.isSelected());
        });
        panel1.add(checkBox3);
// 创建一个面板panel2放置TextArea
        JPanel panel2 = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel2.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        getContentPane().add(panel2, BorderLayout.SOUTH);
// 创建标签
        JLabel lblTextAreaLabel = new JLabel("选择性别：");
// 添加标签到面板panel2
        panel2.add(lblTextAreaLabel);
//创建ButtonGroup对象
        ButtonGroup buttonGroup = new ButtonGroup();
//添加RadioButton到ButtonGroup对象
        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
//添加RadioButton到面板panel2 ⑦
        panel2.add(radioButton1);
        panel2.add(radioButton2);
//注册ItemEvent事件监听器
        radioButton1.addItemListener(this);
        radioButton2.addItemListener(this);
// 设置窗口大小
        pack(); // 紧凑排列，其作用相当于setSize()
// 设置窗口可见
        setVisible(true);
    }
    //实现ItemListener接口方法
   @Override
    public void itemStateChanged(ItemEvent e){
        if(e.getStateChange() == ItemEvent.SELECTED){
            JRadioButton button = (JRadioButton) e.getItem();
            System.out.println(button.getText());
        }
    }
}
